package com.bear.cmcc.NetTestHTTP.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.InfoBean;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 信息列表的适配器
 * @author meizhen.xu
 *
 */
public class InfoListAdapter extends BaseAdapter {
	
	private Context context;
	private List<InfoBean> infoList;
	
	public InfoListAdapter(Context context){
		this.context = context;
		this.infoList = new ArrayList<InfoBean>();
	}

	@Override
	public int getCount() {
		return infoList.size();
	}

	@Override
	public InfoBean getItem(int position) {
		return infoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	/**
	 * 往适配器添加数据
	 * @param b
	 */
	public void addObject(InfoBean b){
		infoList.add(b);
	}

	/**
	 * 清空适配器数据
	 */
	public void clear(){
		infoList.clear();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = LayoutInflater.from(context).inflate(R.layout.item_infolist, null);
		LinearLayout bgLayout = (LinearLayout)view.findViewById(R.id.itemInfoList_bgLayout);
		TextView titleText = (TextView)view.findViewById(R.id.itemInfoList_titleText);
		TextView contentText = (TextView)view.findViewById(R.id.itemInfoList_contentText);
		
		if(position%2==0){
			bgLayout.setBackgroundResource(R.color.infoList_bg01);
		}else{
			bgLayout.setBackgroundResource(R.color.infoList_bg02);
		}
		
		InfoBean b = getItem(position);
		titleText.setText(b.getTitle());
		contentText.setText(b.getAbstr());
		
		return view;
	}

}
